{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Preface\n",
    "\n",
    "## My theory, which is mine\n",
    "\n",
    "The premise of this book, and the other books in the *Think X* series,\n",
    "is that if you know how to program, you can use that skill to learn\n",
    "other topics.\n",
    "\n",
    "Most books on Bayesian statistics use mathematical notation and present\n",
    "ideas in terms of mathematical concepts like calculus. This book uses\n",
    "Python code instead of math, and discrete approximations instead of\n",
    "continuous mathematics. As a result, what would be an integral in a math\n",
    "book becomes a summation, and most operations on probability\n",
    "distributions are simple loops.\n",
    "\n",
    "I think this presentation is easier to understand, at least for people\n",
    "with programming skills. It is also more general, because when we make\n",
    "modeling decisions, we can choose the most appropriate model without\n",
    "worrying too much about whether the model lends itself to conventional\n",
    "analysis.\n",
    "\n",
    "Also, it provides a smooth development path from simple examples to\n",
    "real-world problems. Chapter [estimation] is a good example. It starts\n",
    "with a simple example involving dice, one of the staples of basic\n",
    "probability. From there it proceeds in small steps to the locomotive\n",
    "problem, which I borrowed from Mosteller’s *Fifty Challenging Problems\n",
    "in Probability with Solutions*, and from there to the German tank\n",
    "problem, a famously successful application of Bayesian methods during\n",
    "World War II.\n",
    "\n",
    "## Modeling and approximation\n",
    "\n",
    "Most chapters in this book are motivated by a real-world problem, so\n",
    "they involve some degree of modeling. Before we can apply Bayesian\n",
    "methods (or any other analysis), we have to make decisions about which\n",
    "parts of the real-world system to include in the model and which details\n",
    "we can abstract away.\n",
    "\n",
    "For example, in Chapter [prediction], the motivating problem is to\n",
    "predict the winner of a hockey game. I model goal-scoring as a Poisson\n",
    "process, which implies that a goal is equally likely at any point in the\n",
    "game. That is not exactly true, but it is probably a good enough model\n",
    "for most purposes.\n",
    "\n",
    "In Chapter [evidence] the motivating problem is interpreting SAT scores\n",
    "(the SAT is a standardized test used for college admissions in the\n",
    "United States). I start with a simple model that assumes that all SAT\n",
    "questions are equally difficult, but in fact the designers of the SAT\n",
    "deliberately include some questions that are relatively easy and some\n",
    "that are relatively hard. I present a second model that accounts for\n",
    "this aspect of the design, and show that it doesn’t have a big effect on\n",
    "the results after all.\n",
    "\n",
    "I think it is important to include modeling as an explicit part of\n",
    "problem solving because it reminds us to think about modeling errors\n",
    "(that is, errors due to simplifications and assumptions of the model).\n",
    "\n",
    "Many of the methods in this book are based on discrete distributions,\n",
    "which makes some people worry about numerical errors. But for real-world\n",
    "problems, numerical errors are almost always smaller than modeling\n",
    "errors.\n",
    "\n",
    "Furthermore, the discrete approach often allows better modeling\n",
    "decisions, and I would rather have an approximate solution to a good\n",
    "model than an exact solution to a bad model.\n",
    "\n",
    "On the other hand, continuous methods sometimes yield performance\n",
    "advantages—for example by replacing a linear- or quadratic-time\n",
    "computation with a constant-time solution.\n",
    "\n",
    "So I recommend a general process with these steps:\n",
    "\n",
    "1. While you are exploring a problem, start with simple models and\n",
    "   implement them in code that is clear, readable, and demonstrably\n",
    "   correct. Focus your attention on good modeling decisions, not\n",
    "   optimization.\n",
    "\n",
    "2. Once you have a simple model working, identify the biggest sources of\n",
    "   error. You might need to increase the number of values in a discrete\n",
    "   approximation, or increase the number of iterations in a Monte Carlo\n",
    "   simulation, or add details to the model.\n",
    "\n",
    "3. If the performance of your solution is good enough for your\n",
    "   application, you might not have to do any optimization. But if you\n",
    "   do, there are two approaches to consider. You can review your code\n",
    "   and look for optimizations; for example, if you cache previously\n",
    "   computed results you might be able to avoid redundant computation. Or\n",
    "   you can look for analytic methods that yield computational shortcuts.\n",
    "\n",
    "One benefit of this process is that Steps 1 and 2 tend to be fast, so\n",
    "you can explore several alternative models before investing heavily in\n",
    "any of them.\n",
    "\n",
    "Another benefit is that if you get to Step 3, you will be starting with\n",
    "a reference implementation that is likely to be correct, which you can\n",
    "use for regression testing (that is, checking that the optimized code\n",
    "yields the same results, at least approximately).\n",
    "\n",
    "Working with the code\n",
    "---------------------\n",
    "\n",
    "Many of the examples in this book use classes and functions defined in\n",
    "thinkbayes.py. You can download this module from\n",
    "http://thinkbayes.com/thinkbayes.py.\n",
    "\n",
    "Most chapters contain references to code you can download from\n",
    "http://thinkbayes.com. Some of those files have dependencies you will\n",
    "also have to download. I suggest you keep all of these files in the same\n",
    "directory so they can import each other without changing the Python\n",
    "search path.\n",
    "\n",
    "You can download these files one at a time as you need them, or you can\n",
    "download them all at once from\n",
    "http://thinkbayes.com/thinkbayes_code.zip. This file also contains the\n",
    "data files used by some of the programs. When you unzip it, it creates a\n",
    "directory named ``thinkbayes_code`` that contains all the code used in\n",
    "this book.\n",
    "\n",
    "Or, if you are a Git user, you can get all of the files at once by\n",
    "forking and cloning this repository:\n",
    "https://github.com/AllenDowney/ThinkBayes.\n",
    "\n",
    "One of the modules I use is thinkplot.py, which provides wrappers for\n",
    "some of the functions in pyplot. To use it, you need to install\n",
    "matplotlib. If you don’t already have it, check your package manager to\n",
    "see if it is available. Otherwise you can get download instructions from\n",
    "http://matplotlib.org.\n",
    "\n",
    "Finally, some programs in this book use NumPy and SciPy, which are\n",
    "available from http://numpy.org and http://scipy.org.\n",
    "\n",
    "## Code style\n",
    "\n",
    "Experienced Python programmers will notice that the code in this book\n",
    "does not comply with PEP 8, which is the most common style guide for\n",
    "Python (http://www.python.org/dev/peps/pep-0008/).\n",
    "\n",
    "Specifically, PEP 8 calls for lowercase function names with underscores\n",
    "between words, ``like_this``. In this book and the accompanying code,\n",
    "function and method names begin with a capital letter and use camel\n",
    "case, ``LikeThis``.\n",
    "\n",
    "I broke this rule because I developed some of the code while I was a\n",
    "Visiting Scientist at Google, so I followed the Google style guide,\n",
    "which deviates from PEP 8 in a few places. Once I got used to Google\n",
    "style, I found that I liked it. And at this point, it would be too much\n",
    "trouble to change.\n",
    "\n",
    "Also on the topic of style, I write “Bayes’s theorem” with an *s* after\n",
    "the apostrophe, which is preferred in some style guides and deprecated\n",
    "in others. I don’t have a strong preference. I had to choose one, and\n",
    "this is the one I chose.\n",
    "\n",
    "And finally one typographical note: throughout the book, I use PMF and\n",
    "CDF for the mathematical concept of a probability mass function or\n",
    "cumulative distribution function, and Pmf and Cdf to refer to the Python\n",
    "objects I use to represent them.\n",
    "\n",
    "## Prerequisites\n",
    "\n",
    "There are several excellent modules for doing Bayesian statistics in\n",
    "Python, including pymc and OpenBUGS. I chose not to use them for this\n",
    "book because you need a fair amount of background knowledge to get\n",
    "started with these modules, and I want to keep the prerequisites\n",
    "minimal. If you know Python and a little bit about probability, you are\n",
    "ready to start this book.\n",
    "\n",
    "Chapter [intro] is about probability and Bayes’s theorem; it has no\n",
    "code. Chapter [compstat] introduces Pmf, a thinly disguised Python\n",
    "dictionary I use to represent a probability mass function (PMF). Then\n",
    "Chapter [estimation] introduces Suite, a kind of Pmf that provides a\n",
    "framework for doing Bayesian updates. And that’s just about all there is\n",
    "to it.\n",
    "\n",
    "Well, almost. In some of the later chapters, I use analytic\n",
    "distributions including the Gaussian (normal) distribution, the\n",
    "exponential and Poisson distributions, and the beta distribution. In\n",
    "Chapter [species] I break out the less-common Dirichlet distribution,\n",
    "but I explain it as I go along. If you are not familiar with these\n",
    "distributions, you can read about them on Wikipedia. You could also read\n",
    "the companion to this book, *Think Stats*, or an introductory statistics\n",
    "book (although I’m afraid most of them take a mathematical approach that\n",
    "is not particularly helpful for practical purposes).\n",
    "\n",
    "## Contributor List\n",
    "\n",
    "If you have a suggestion or correction, please send email to\n",
    "*downey@allendowney.com*. If I make a change based on your feedback, I\n",
    "will add you to the contributor list (unless you ask to be omitted).\n",
    "\n",
    "If you include at least part of the sentence the error appears in, that\n",
    "makes it easy for me to search. Page and section numbers are fine, too,\n",
    "but not as easy to work with. Thanks!\n",
    "\n",
    "-  First, I have to acknowledge David MacKay’s excellent book,\n",
    "   *Information Theory, Inference, and Learning Algorithms*, which is\n",
    "   where I first came to understand Bayesian methods. With his\n",
    "   permission, I use several problems from his book as examples.\n",
    "\n",
    "-  This book also benefited from my interactions with Sanjoy Mahajan,\n",
    "   especially in fall 2012, when I audited his class on Bayesian\n",
    "   Inference at Olin College.\n",
    "\n",
    "-  I wrote parts of this book during project nights with the Boston\n",
    "   Python User Group, so I would like to thank them for their company\n",
    "   and pizza.\n",
    "\n",
    "-  Jonathan Edwards sent in the first typo.\n",
    "\n",
    "-  George Purkins found a markup error.\n",
    "\n",
    "-  Olivier Yiptong sent several helpful suggestions.\n",
    "\n",
    "-  Yuriy Pasichnyk found several errors.\n",
    "\n",
    "-  Kristopher Overholt sent a long list of corrections and suggestions.\n",
    "\n",
    "-  Robert Marcus found a misplaced *i*.\n",
    "\n",
    "-  Max Hailperin suggested a clarification in Chapter [intro].\n",
    "\n",
    "-  Markus Dobler pointed out that drawing cookies from a bowl with\n",
    "   replacement is an unrealistic scenario.\n",
    "\n",
    "-  Tom Pollard and Paul A. Giannaros spotted a version problem with some\n",
    "   of the numbers in the train example.\n",
    "\n",
    "-  Ram Limbu found a typo and suggested a clarification.\n",
    "\n",
    "-  In spring 2013, students in my class, Computational Bayesian\n",
    "   Statistics, made many helpful corrections and suggestions: Kai\n",
    "   Austin, Claire Barnes, Kari Bender, Rachel Boy, Kat Mendoza, Arjun\n",
    "   Iyer, Ben Kroop, Nathan Lintz, Kyle McConnaughay, Alec Radford,\n",
    "   Brendan Ritter, and Evan Simpson.\n",
    "\n",
    "-  Greg Marra and Matt Aasted helped me clarify the discussion of *The\n",
    "   Price is Right* problem.\n",
    "\n",
    "-  Marcus Ogren pointed out that the original statement of the\n",
    "   locomotive problem was ambiguous.\n",
    "\n",
    "-  Jasmine Kwityn and Dan Fauxsmith at O’Reilly Media proofread the book\n",
    "   and found many opportunities for improvement.\n",
    "\n",
    "-  James Lawry spotted a math error.\n",
    "\n",
    "-  Ben Kahle found a reference to the wrong figure.\n",
    "\n",
    "-  Jeffrey Law found an inconsistency between the text and the code.\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
